package cn.seaboot.admin.se;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.function.Supplier;

/**
 * @author Mr.css
 * @version 2024-11-22 10:26
 */
public class Selenium {
    private final Logger logger = LoggerFactory.getLogger(Selenium.class);

    public Selenium() {

        logger.debug("This is a debug message");
    }

    public ChromeDriver getDriver() {
        ChromeOptions chromeOptions = new ChromeOptions();
        // 取消机器控制这一栏（启用一定程度上会挤压界面）
//        chromeOptions.addArguments("disable-infobars");
        // 指定访问 ip
//        chromeOptions.addArguments("--allowed-ips");

        // 禁用 gpu
//        chromeOptions.addArguments("--disable-gpu");
        // 无视控制台报错信息
        chromeOptions.addArguments("--ignore-certificate-errors");
        // 取消沙盒模式
        chromeOptions.addArguments("--no-sandbox");

        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();


        return driver;
    }

    /**
     * -
     */
    public void doWork() {
        ChromeDriver driver = this.getDriver();
        try {
            driver.get("http://119.45.132.175:8087/gl-nms/web/#/login");

            this.login(driver, "admin", "makermaker");


        } finally {
            if (driver != null) {
                driver.quit();
            }
        }
    }

    /**
     * 登录百度
     *
     * @param driver 驱动
     */
    public void openBaidu(ChromeDriver driver) {
        driver.get("https://www.baidu.com");

        System.out.print(driver.getTitle());
        System.out.print(driver.getPageSource());
    }


    /**
     * 登录
     *
     * @param driver   浏览器驱动
     * @param username 账号
     * @param password 密码
     */
    private void login(ChromeDriver driver, String username, String password) {
        logger.debug("login: {}", username);

        // 输入账号密码
        List<WebElement> elements = driver.findElementsByTagName("input");
        if (elements.size() != 2) {
            throw new RuntimeException("Login page has changed! Should update this Java code!");
        }

        WebElement usernameInput = elements.get(0);
        WebElement passwordInput = elements.get(1);

        usernameInput.sendKeys(username);
        passwordInput.sendKeys(password);

        // 点击登录按钮
        elements = driver.findElementsByTagName("button");
        WebElement submitButton = elements.get(0);
        submitButton.click();

        logger.debug("submit login page!");
    }


    public <T> T work(Supplier<T> supplier) {
        ChromeDriver driver = this.getDriver();
        try {
            return supplier.get();
        } finally {
            if (driver != null) {
                driver.quit();
            }
        }
    }


    public static void main(String[] args) throws InterruptedException {
        Selenium selenium = new Selenium();
//        selenium.doWork();
    }
}
